<?php

namespace app\model\game;

use app\base\BaseModel;
use app\libs\Util;
use app\model\user\UserBeanLogModel;
use app\model\user\UserCouponModel;

/**
 * 模型-GameUserModel
 * Class GameUser
 * @package app\model
 */
class GameUserModel extends BaseModel
{
    protected $name = 'game_user';

    /**
     * 添加中奖
     * @param int $user_id
     * @param int $type 1铜卡 2银卡 3金卡 4钻卡 5铜盲盒 6银盲盒 7金盲盒 8钻盲盒
     * @return bool
     */
    public static function addCard($user_id = 0, $type = 1)
    {
        $name = "";
        $class = 1; //1卡片 2礼盒
        switch ($type) {
            case 1:
                $name = "铜卡";
                $class = 1;
                break;
            case 2:
                $name = "银卡";
                $class = 1;
                break;
            case 3:
                $name = "金卡";
                $class = 1;
                break;
            case 4:
                $name = "钻卡";
                $class = 1;
                break;
            case 5:
                $name = "铜礼盒";
                $class = 2;
                break;
            case 6:
                $name = "银礼盒";
                $class = 2;
                break;
            case 7:
                $name = "金礼盒";
                $class = 2;
                break;
            case 8:
                $name = "钻礼盒";
                $class = 2;
                break;
        }
        $in_data = [
            'add_time' => time(),
            'user_id' => $user_id,
            'name' => $name,
            'number' => Util::getRandomString(10),
            'type' => $type,
            'class' => $class,
            'detail' => "盲盒抽奖获得"
        ];
        $ret = self::insert($in_data);
        if ($ret == false) {
            return false;
        }
        return true;
    }

    /**
     * 强行开启卡片
     * @param int $user_id
     * @param int $type
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function useCard($user_id = 0, $type = 1)
    {
        $type_list = GameTypeModel::getTypeList();
        $type_temp = $type_list[intval($type)];
        $coupon = $type_temp['coupon'];
        $name = $type > 4 ? "卡片" : "礼盒";
        $map = [
            'user_id' => $user_id,
            'type' => $type,
            'use' => 1
        ];
        $card_item = self::where($map)->find();
        if (empty($card_item)) {
            return [
                'code' => 0,
                'msg' => '操作失败'
            ];
        }
        $ret = true;
        if ($coupon > 50) {
            for ($i = 0; $i < intval($coupon / 50); $i++) {
                $ret = UserCouponModel::addCoupon($user_id, 50, 1, "开启" . $name . "获得", "开启" . $name);
            }
            if (($coupon % 50) > 0) {
                $ret = UserCouponModel::addCoupon($user_id, ($coupon % 50), 1, "开启" . $name . "获得", "开启" . $name);
            }
        }
        if ($ret !== false) {
            $bean = $type_temp['bean'];
            $out_data = UserBeanLogModel::changeEarningsBean($user_id, $bean, "return", '开' . $name . '获得 ');
            if (empty($out_data['code'])) {
                return $out_data;
            }
            $id = $card_item['id'];
            $up_data = [
                'use' => 2,
                'use_time' => time()
            ];
            $ret = self::where('id', $id)->update($up_data);
            if ($ret == false) {
                return [
                    'code' => 0,
                    'msg' => '操作失败'
                ];
            }
            return $out_data;
        }
        return [
            'code' => 0,
            'msg' => '操作失败'
        ];
    }

    /**
     * 合成卡片 变成礼盒
     * @param int $user_id
     * @param int $type
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function comCard($user_id = 0, $type = 1)
    {
//        $type_list = GameTypeModel::getTypeList();
//        $type_temp = $type_list[intval($type)];
        $card_data = self::getCardList($user_id, $type);
        if (intval($card_data['count']) >= 5) {
            $card_ids = $card_data['card_ids'];
            $card_ids_end = [];
            for ($i = 0; $i < 5; $i++) {
                $card_ids_end[] = $card_ids[$i];
            }
            $up_data = [
                'use' => 2,
                'use_time' => time()
            ];
            $ret = self::whereIn('id', $card_ids_end)->update($up_data);
            if ($ret === false) {
                return [
                    'code' => 0,
                    'msg' => '合成失败'
                ];
            }
            $ret = self::addCard($user_id, ($type + 4));
            if ($ret === false) {
                self::addCard($user_id, ($type + 4));
            }
            return [
                'code' => 1,
                'msg' => '合成功成功'
            ];
        } else {
            return [
                'code' => 0,
                'msg' => '卡片数量不够'
            ];
        }
    }

    /**
     * 获取我的道具列表
     * @param int $user_id
     * @param int $type
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getCardList($user_id = 0, $type = 0)
    {
        $map = [
            'user_id' => $user_id,
            'use' => 1
        ];
        if (!empty($type)) {
            $map['type'] = $type;
            $card_list = self::where($map)->select();
            if (empty($card_list)) {
                return [];
            }
            $card_ids = [];
            $count = 0;
            foreach ($card_list as $value) {
                $card_ids[] = $value['id'];
                $count++;
            }
            return [
                'name' => $card_list[0]['name'],
                'type' => $card_list[0]['type'],
                'card_ids' => $card_ids,
                'count' => $count
            ];
        }
        $card_list = self::where($map)->select();
        $card_data = [];
        foreach ($card_list as $value) {
            if (!isset($card_data[intval($value['type'])])) {
                $card_data[intval($value['type'])] = [
                    'name' => $value['name'],
                    'type' => $value['type'],
                    'count' => 1
                ];
            } else {
                $card_data[intval($value['type'])]['count'] += 1;
            }
        }
        return $card_data;
    }

    /**
     * 获取我的卡盒列表
     * @param int $user_id
     * @return GameUserModel|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getCardItemList($user_id = 0)
    {
        $map = [
            'user_id' => $user_id,
            'use' => 1
        ];
        $card_list = self::where($map)->field('id,type,class')->select();
        if (empty($card_list)) {
            return [];
        }
        return $card_list;
    }

    /**
     * 获取卡盒数量
     * @param string $user_id
     * @return int
     * @throws \think\db\exception\DbException
     */
    public static function getCard($user_id = '')
    {
        $map = [
            'user_id' => $user_id,
            'use' => 1,
        ];
        $count = self::where($map)->count('id');
        if (empty($count)) {
            return 0;
        }
        return $count;
    }

    /**
     * 获取可用卡片张数
     * @param string $user_id
     * @param int $type
     * @return int
     */
    public static function getCardNum($user_id = '', $type = 0)
    {
        $map = [
            'user_id' => $user_id,
            'type' => $type,
            'use' => 1
        ];
        return self::where($map)->count('id');
    }

    /**
     * 获取卡、盒个数
     * @param string $user_id
     * @param int $class
     * @return int
     */
    public static function getCardClassNum($user_id = '', $class = 0)
    {
        if (intval($class) == 1) {
            $map1 = [
                'user_id' => $user_id,
                'win' => 2,
                'settlement' => 0
            ];
            $game_process_model = new GameProcessModel();
            $card = $game_process_model->where($map1)->count('id');
        } else {
            $card = 0;
        }
        $map = [
            'user_id' => $user_id,
            'class' => $class,
            'use' => 1
        ];
        return self::where($map)->count('id') + $card;
    }

    /**
     * 获取当前未结算卡片的扣除趣豆
     * @param string $user_id
     * @return int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getCardBean($user_id = '')
    {
        $map = [
            'user_id' => $user_id,
            'win' => 2,
            'settlement' => 0
        ];
        $type_list = GameTypeModel::getTypeList();
        $game_process_model = new GameProcessModel();
        $car_list = $game_process_model->where($map)->field('type')->select();
        $bean_sum = 0;
        foreach ($car_list as $value) {
            $type_temp = $type_list[intval($value['type'])];
            $bean_sum += $type_temp['bean'];
        }
        return $bean_sum;
    }
}